package org.jxc.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.jxc.domain.Customer;
import org.team4.dao.GenericDAO;
import org.team4.domain.Users;
import org.team4.exception.CRUDException;
import org.team4.util.LoginBean;

public class CustomerDAO extends GenericDAO<Customer> {

	private static final long serialVersionUID = 376038639038947152L;
	private static CustomerDAO instance;

	public static CustomerDAO getInstance(SessionFactory sessionFactory) {
		if (instance == null) {
			instance = new CustomerDAO(sessionFactory);
		}
		return instance;
	}

	private CustomerDAO(SessionFactory sessionFactory) {
		super(sessionFactory, Customer.class);
	}

	public List<Customer> findByOperator() throws CRUDException {
		Users user = LoginBean.getLoginBean().getUser();
		List<Customer> list = null;
		if (user.getDepartment().getManager() == user) {
			list = instance.findAll();
		} else {
			list = instance.find(
					"from Customer where deleteFlag = '0' and operator.id=?",
					user.getId());
		}
		return list;
	}
}